package com.yuyou.train.user.controller;

import com.yuyou.train.service.api.user.entity.User;
import com.yuyou.train.user.service.IUserService;
import com.yuyou.train.user.vo.resp.*;
import com.yuyou.train.util.bean.*;
import com.yuyou.train.util.tool.BeanUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;  import org.slf4j.LoggerFactory;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yuyou.train.service.api.user.dto.UserMedalCategoryDTO;
import com.yuyou.train.service.api.user.dto.UserMedalItemDetailDTO;
import com.yuyou.train.service.api.user.dto.UserMedalRelationDTO;
import com.yuyou.train.service.api.user.entity.UserMedalCategory;
import com.yuyou.train.service.api.user.entity.UserMedalItem;
import com.yuyou.train.user.service.IUserMedalCategoryService;
import com.yuyou.train.user.service.IUserMedalItemService;
import com.yuyou.train.user.service.IUserMedalRelationService;
import com.yuyou.train.user.vo.req.ReqMedalItemUp;
import com.yuyou.train.user.vo.req.ReqMedalUp;
import com.yuyou.train.util.tool.DefindBeanUtils;

import javax.servlet.http.HttpServletRequest;

@Validated
@RestController
@RequestMapping(value="/api/userMedal")
public class UserMedalController extends BaseController{

	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private IUserMedalCategoryService userMedalCategoryService;
	
	@Autowired
	private IUserMedalItemService userMedalItemService;
	
	@Autowired
	IUserMedalRelationService userMedalRelationService;

	@Autowired
	private IUserService userService;
		
	@ApiOperation(value="更新勋章类别")
	@RequestMapping(value="/updateCategory",method=RequestMethod.POST)
	public ReturnResult<ResBoolSimpleInfo<String>> updateCategory(@Validated @ModelAttribute ReqMedalUp reqMedalUp){
		UserMedalCategory userMedalCategory=new UserMedalCategory();
		DefindBeanUtils.copyProperties(reqMedalUp, userMedalCategory);
		userMedalCategoryService.updateByPrimaryKeySelective(userMedalCategory);
		return ReturnResult.success();
	}
		
	@ApiOperation(value="勋章类别分页")
	@RequestMapping(value="/findMedalCategoryPage" ,method=RequestMethod.POST)
	public ReturnResult<Pagination<UserMedalCategoryDTO>> findMedalCategoryPage(@Validated @ModelAttribute PageParams pageParams){
		Map<String, Object> conditions=new HashMap<String, Object>();
		Pagination<UserMedalCategoryDTO> queryPgn = new Pagination<UserMedalCategoryDTO>();
		queryPgn.setConditions(conditions);
		queryPgn.setPageParams(pageParams);
		int total =userMedalCategoryService.findMedalCategoryPageCount(queryPgn);
		queryPgn.setTotal(total);
		queryPgn.toBePage();
		if(total>0){
			queryPgn.setTotal(total);
			List<UserMedalCategoryDTO> list=userMedalCategoryService.findMedalCategoryPageList(queryPgn);
			queryPgn.setList(list);
		}
		return ReturnResult.success(queryPgn);
	}
	
	@ApiOperation(value="勋章类别详情")
	@ApiImplicitParam(name = "id", value = "类别ID", paramType="query", required = true)
	@RequestMapping(value="/DetailCategory",method=RequestMethod.POST)
	public ReturnResult<UserMedalCategoryDTO> DetailCategory(
			@NotEmpty (message="id不能为空") String id){
		UserMedalCategoryDTO c=userMedalCategoryService.selectByPrimaryKey(id);
		return ReturnResult.success(c);
	}
	
	@ApiOperation(value="勋章明细详情")
	@ApiImplicitParam(name = "id", value = "明细ID", paramType="query", required = true)
	@RequestMapping(value="/DetailItem",method=RequestMethod.POST)
	public ReturnResult<UserMedalItemDetailDTO> DetailItem(
			@NotEmpty (message="id不能为空") String id){
		UserMedalItemDetailDTO userMedalItem=userMedalItemService.selectByPrimaryKey(id);
		return ReturnResult.success(userMedalItem);
	}
	
	
	
	
	@ApiOperation(value="更新勋章明细")
	@RequestMapping(value="/updateMedalItem",method=RequestMethod.POST)
	public ReturnResult<ResBoolSimpleInfo<String>> addMedalItem(@Validated @ModelAttribute ReqMedalItemUp reqMedalItemUp){
		UserMedalItem userMedalItem=new UserMedalItem();
		DefindBeanUtils.copyProperties(reqMedalItemUp, userMedalItem);
		userMedalItemService.updateByPrimaryKeySelective(userMedalItem);	
		return ReturnResult.success();
	}

	@ApiOperation(value="勋章明细分页")
	@RequestMapping(value="/findMedalItemPage" ,method=RequestMethod.POST)
	public ReturnResult<Pagination<UserMedalItemDetailDTO>> findMedalItemPage(
			@Validated @ModelAttribute PageParams pageParams){
		Map<String, Object> conditions=new HashMap<String, Object>();
		Pagination<UserMedalItemDetailDTO> queryPgn = new Pagination<UserMedalItemDetailDTO>();
		queryPgn.setConditions(conditions);
		queryPgn.setPageParams(pageParams);
		int total =userMedalItemService.findUserMedalItemPageCount(queryPgn);
		queryPgn.setTotal(total);
		queryPgn.toBePage();
		if(total>0){
			queryPgn.setTotal(total);
			List<UserMedalItemDetailDTO> list=userMedalItemService.findUserMedalItemPageList(queryPgn);
			queryPgn.setList(list);
		}
		return ReturnResult.success(queryPgn);
	}
	
	@ApiOperation(value="查找用户的勋章(前台)")
	@RequestMapping(value="/findUserMedalItemPage", method=RequestMethod.POST)
	public ReturnResult<Pagination<UserMedalRelationDTO>> findUserMedalItemPage(@Validated @ModelAttribute PageParams pageParams,
																				HttpServletRequest request){
		LoginRedisObj loginObj = this.getUserObj(request);
		Pagination<UserMedalRelationDTO> userMedalPage = userMedalItemService.findUserMedalPage(pageParams, loginObj.getUserId(), null);
		return ReturnResult.success(userMedalPage);
	}

	@ApiOperation(value = "勋章排行榜(前台)")
	@RequestMapping(value="/findUserMedalTop", method = RequestMethod.POST)
	public ReturnResult<RespUserMedalRank> findUserMedalTop(HttpServletRequest request){

		LoginRedisObj loginObj = this.getUserObj(request);
		User user = userService.selectByPrimaryKey(loginObj.getUserId());

		RespUserMedalRank userMedalRank = new RespUserMedalRank();
		List<RespUserMedalTop> userMedalTopList = userMedalRelationService.findUserMedalTop(user.getClassId());
		Integer myRank = userMedalRelationService.myRank(user.getClassId(), user.getId());

		// 处理勋章数一样时排名问题
		List<String> userIds = BeanUtils.toList(userMedalTopList, "userId");
		if(!StringUtils.isEmpty(userIds) && !StringUtils.isEmpty(userMedalTopList) && userIds.contains(user.getId())){
			for(int i = 0; i < userMedalTopList.size(); i++){
				if(user.getId().equals(userMedalTopList.get(i).getUserId())){
					myRank = i + 1;
					break;
				}
			}
		}

		userMedalRank.setUserMedalTopList(userMedalTopList);
		userMedalRank.setMyRank(myRank);
		return ReturnResult.success(userMedalRank);
	}


	@ApiOperation(value = "我的成就(前台)")
	@RequestMapping(value="/myMedalList", method = RequestMethod.POST)
	public ReturnResult<List<RespMyMedalCategory>> myMedalList(HttpServletRequest request){

		LoginRedisObj loginObj = this.getUserObj(request);
		List<RespMyMedalCategory> myMedalCategoryList = userMedalRelationService.findMyMedal(loginObj.getUserId());

		return ReturnResult.success(myMedalCategoryList);
	}
}
